﻿#include "camera_qdialog.h"
//#include "hk_camera_plugin.h"
#include "qpluginloader.h"
#include "camera_dh.h"
#include <QDesktopWidget>
//typedef CameraInterface* (*Fuction)();

CameraBaseQDialog::CameraBaseQDialog(QDialog *parent, QSettings* qsettings,int camera_index) : QDialog(parent)
{
//    move(0,0);
//    setFixedSize(1920,1000);
    m_camera_index = camera_index;

    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width() / 1920.0;
    double ratio_y = desktop_rect.height() / 1080.0;

    move(0, 0);
    setFixedSize(1880 * ratio_x, 910 * ratio_y);

//    this->setModal(true);
    QGridLayout *qgridLayout = new QGridLayout(this);


    QVBoxLayout *qgridLayout_left = new QVBoxLayout();

	m_qsettings = qsettings;
    m_section = "camera" + QString::number(camera_index);


    QSize control_size(120,40);

    if(m_camera_index==2 || m_camera_index==3||m_camera_index==4)
    {
        /*m_qlabel_exposure_time = new QLabel();
        m_qlabel_exposure_time->setText("曝光时长");
        m_qlabel_exposure_time->setFixedSize(control_size);
        qgridLayout_left->addWidget(m_qlabel_exposure_time,0,0);

        m_qspinbox_exposure_time = new QSpinBox();
        m_qspinbox_exposure_time->setFixedSize(control_size);
        m_qspinbox_exposure_time->setMaximum(100000);
        qgridLayout_left->addWidget(m_qspinbox_exposure_time,1,0);

        m_qlabel_gain = new QLabel();
        m_qlabel_gain->setText("增益");
        m_qlabel_gain->setFixedSize(control_size);
        qgridLayout_left->addWidget(m_qlabel_gain,2,0);

        m_qspinbox_gain = new QSpinBox();
        m_qspinbox_gain->setFixedSize(control_size);
        m_qspinbox_gain->setMinimum(0);
        m_qspinbox_gain->setMaximum(1000);
        qgridLayout_left->addWidget(m_qspinbox_gain,3,0);*/

        m_qlabel_trigger_mode = new QLabel();
        m_qlabel_trigger_mode->setText("camera trigger");
        m_qlabel_trigger_mode->setFixedSize(control_size);
        qgridLayout_left->addWidget(m_qlabel_trigger_mode,4,0);

        m_qcombox_trigger_mode = new QComboBox();
        m_qcombox_trigger_mode->setFixedSize(control_size);
        m_qcombox_trigger_mode->addItem("Off");
        m_qcombox_trigger_mode->addItem("On");
        qgridLayout_left->addWidget(m_qcombox_trigger_mode,5,0);

        m_qlabel_trigger_source = new QLabel();
        m_qlabel_trigger_source->setText("camera trigger source");
        m_qlabel_trigger_source->setFixedSize(control_size);
        qgridLayout_left->addWidget(m_qlabel_trigger_source,6,0);

        m_qcombox_trigger_source = new QComboBox();
        m_qcombox_trigger_source->setFixedSize(control_size);
        m_qcombox_trigger_source->addItem("Line0");
        m_qcombox_trigger_source->addItem("Line1");
        m_qcombox_trigger_source->addItem("Line2");
        m_qcombox_trigger_source->addItem("Software");
        qgridLayout_left->addWidget(m_qcombox_trigger_source,7,0);
    }


    m_qpushbutton_soft_triger = new QPushButton();
    m_qpushbutton_soft_triger->setFixedSize(control_size);
    m_qpushbutton_soft_triger->setText("soft trigger");
    qgridLayout_left->addWidget(m_qpushbutton_soft_triger,8,0);
    connect(m_qpushbutton_soft_triger, &QPushButton::clicked, this, &CameraBaseQDialog::softTrigger);

    m_qpushbutton_save = new QPushButton();
    m_qpushbutton_save->setFixedSize(control_size);
    m_qpushbutton_save->setText("save");
    qgridLayout_left->addWidget(m_qpushbutton_save,9,0);
    connect(m_qpushbutton_save, &QPushButton::clicked, this, &CameraBaseQDialog::saveParameter);

    m_qpushbutton_hide = new QPushButton();
    m_qpushbutton_hide->setFixedSize(control_size);
    m_qpushbutton_hide->setText("exit");
    qgridLayout_left->addWidget(m_qpushbutton_hide,10,0);

//    connect(m_qpushbutton_hide,&QPushButton::clicked,this,&CameraBaseQDialog::hide);
    qgridLayout->addLayout(qgridLayout_left,0,0);

    m_showqlable = new ShowQLabel(nullptr);
    m_showqlable->setFixedSize(1400* ratio_x,800* ratio_y);
    qgridLayout->addWidget(m_showqlable,0,1);

    QTimer* m_qtimer = new QTimer(this);
    connect(m_qtimer, &QTimer::timeout, this, [=]() {
        m_mutex.lock();
        Mat image = m_image_show.clone();
        m_image_show.release();
        QString message = m_message;
        m_mutex.unlock();
        if(image.empty())
        {
            return;
        }

        double ratio_x =  double(m_showqlable->width()) /  double(image.cols) ;
        double ratio_y =  double(m_showqlable->height()) /  double(image.rows) ;
        double ratio_image = ratio_x < ratio_y ?  ratio_x : ratio_y;

        cv::resize(image, image, Size(image.cols * ratio_image , image.rows * ratio_image));

        QImage q_image = matToQImage(image);
        if (!q_image.isNull())
        {
            m_showqlable->loadtextimage(q_image,message,QColor(0,255,0));
        }
        });
    m_qtimer->start(30);
}

bool CameraBaseQDialog::initCamera(QString type,QString name,CameraCallback pfunction, void *pcontext)
{
    logger->info("initCamera");
    m_camera_interface = new DhCameraInterface();
    std::string string_name = name.toLocal8Bit().toStdString();
    bool state = m_camera_interface->camera_init( (char*)string_name.c_str(),pfunction, pcontext);
    if(state == false)
    {
        return false;
    }


    loadParameter();
    return true;
}

void CameraBaseQDialog::loadParameter()
{
    if(m_camera_index==2 || m_camera_index==3||m_camera_index==4)
    {
       /* int qspinbox_exposure_time =m_qsettings->value(m_section + "/" + "m_qspinbox_exposure_time", 0).toInt();
        int qspinbox_gain = m_qsettings->value(m_section + "/" + "m_qspinbox_gain", 0).toInt();*/
        QString qcombox_trigger_mode = m_qsettings->value(m_section + "/" + "m_qcombox_trigger_mode", 0).toString();
        QString qcombox_trigger_source = m_qsettings->value(m_section + "/" + "m_qcombox_trigger_source", 0).toString();


       /* m_qspinbox_exposure_time->setValue(m_qsettings->value(m_section + "/" + "m_qspinbox_exposure_time", 0).toInt());
        m_qspinbox_gain->setValue(m_qsettings->value(m_section + "/" + "m_qspinbox_gain", 0).toInt());*/
         m_qcombox_trigger_mode->setCurrentText(qcombox_trigger_mode);
        m_qcombox_trigger_source->setCurrentText(qcombox_trigger_source);

        bool state;

        /*state = m_camera_interface->setExposureTime234(m_qspinbox_exposure_time->value());
        if(state==false)
        {
                QMessageBox::critical(nullptr, m_camera_name, "ExposureTime");

        }


        state = m_camera_interface->setGainValue234(m_qspinbox_gain->value());
        if(state==false)
        {
                QMessageBox::critical(nullptr, m_camera_name, "Gain");
                return;
        }*/

		

        state = m_camera_interface->setTriggerMode234(m_qcombox_trigger_mode->currentText().toStdString());
        if(state==false)
        {
                QMessageBox::critical(nullptr, m_camera_name, m_qcombox_trigger_mode->currentText());
                return;
        }

        state = m_camera_interface->setTriggerSource234(m_qcombox_trigger_source->currentText().toStdString());
        if(state==false)
        {
               QMessageBox::critical(nullptr, m_camera_name, m_qcombox_trigger_source->currentText());
                return;
        }

    }
}

void CameraBaseQDialog::saveParameter()
{
    if(m_camera_interface == nullptr)
    {
        QMessageBox::critical(nullptr, m_camera_name, "相机初始化失败");
        return;
    }

    bool state;
    if(m_camera_index==2 || m_camera_index==3||m_camera_index==4)
    {

        //state = m_camera_interface->setExposureTime234(m_qspinbox_exposure_time->value());
		/*state = m_camera_interface->setExposureTime234(1000.0);
        if(state==false)
        {
           QMessageBox::critical(nullptr, m_camera_name, "setExposureTime");
        }

        state = m_camera_interface->setGainValue234(m_qspinbox_gain->value());
        if(state==false)
        {
            QMessageBox::critical(nullptr, m_camera_name, "setGain");
            return;
        }*/


        state = m_camera_interface->setTriggerMode234(m_qcombox_trigger_mode->currentText().toStdString());
        if(state==false)
        {
            QMessageBox::critical(nullptr, m_camera_name, "setTriggerMode");
            return;
        }

        state = m_camera_interface->setTriggerSource234(m_qcombox_trigger_source->currentText().toStdString());
        if(state==false)
        {
            QMessageBox::critical(nullptr, m_camera_name, "trigger_source");
            return;
        }

        if(m_camera_index==2 || m_camera_index==3||m_camera_index==4)
        {
           /* m_qsettings->setValue(m_section + "/" + "m_qspinbox_exposure_time", m_qspinbox_exposure_time->value());
            m_qsettings->setValue(m_section + "/" + "m_qspinbox_gain", m_qspinbox_gain->value());*/
            m_qsettings->setValue(m_section + "/" + "m_qcombox_trigger_mode", m_qcombox_trigger_mode->currentText());
            m_qsettings->setValue(m_section + "/" + "m_qcombox_trigger_source", m_qcombox_trigger_source->currentText());
        }
    }

}

void CameraBaseQDialog::showImage(Mat image,int camera_index,int index)
{
    m_mutex.lock();
    m_image_show = image;
    m_message = m_type + " " + m_camera_name + "  "  + QString::number(index);
    m_mutex.unlock();

}

void CameraBaseQDialog::softTrigger()
{

    if(m_camera_interface == nullptr)
    {
        QMessageBox::critical(nullptr, m_section, "相机初始化失败");
        return;
    }

    if(m_camera_index<=1)
    {
        bool state = m_camera_interface->softwareTrigger01();
        if(state==false)
            QMessageBox::critical(nullptr, m_camera_name,"软触发失败");;
    }

    if(m_camera_index>1)
    {
        bool state = m_camera_interface->softwareTrigger234();
        if(state==false)
            QMessageBox::critical(nullptr, m_section,"软触发");;
    }


}
